package com.fzkj.algorithm;

import java.util.ArrayDeque;

/**
 * 二叉树层次遍历
 */
public class Tree {


    public static void main(String[] args) {
        ArrayDeque<Node> queue = new ArrayDeque<>();
        Node node = Node.build();
        queue.add(node);

        while (!queue.isEmpty()){
            Node node1 = queue.poll();
            System.out.printf(node1.getValue() + " ");
            if (node1.getLeft() != null){
                queue.add(node1.getLeft());
            }
            if (node1.getRight() != null){
                queue.add(node1.getRight());
            }
        }
    }

}

class Node {
    Node left;
    Node right;

    int value;

    public Node(Node left, Node right, int value) {
        this.left = left;
        this.right = right;
        this.value = value;
    }

    public Node getLeft() {
        return left;
    }

    public Node getRight() {
        return right;
    }

    public int getValue() {
        return value;
    }

    public static Node build(){
        Node seven = new Node(null, null, 5);
        Node six = new Node(null, null, 4);
        Node five = new Node(null, null, 2);
        Node four = new Node(null, null, 2);
        Node three = new Node(seven, null, 5);
        Node two = new Node(five, six, 4);
        Node one = new Node(three, four, 2);
        return new Node(one, two, 3);
    }
}
